package com.xwj.kafka

import java.util
import java.util.Properties

import scala.collection.JavaConverters._
import org.apache.kafka.clients.consumer.{ KafkaConsumer}

/**
  * Created by thinkjoy on 2017/8/10.
  * 消费者
  */
object KafkaConsumer {

  def main(args: Array[String]): Unit = {
    val TOPIC="thinkjoys"
    val props=new Properties()
    props.put("bootstrap.servers","192.168.0.78:9092")
    props.put("zookeeper.connect","192.168.0.78:2181,192.168.0.75:2181,192.168.0.76:2181")
    props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
    props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
    props.put("group.id", "1")
    props.put("enable.auto.commit", "true")
    props.put("auto.commit.interval.ms", "1000")
    props.put("session.timeout.ms", "30000")
    props.put("enable.auto.commit", "true")
    val consumer=new KafkaConsumer[String,String](props)
    consumer.subscribe(util.Arrays.asList(TOPIC))
    while (true){
      val records=consumer.poll(100)
      for(record <- records.asScala){
        println("消费者消费数据:"+record)
      }
    }
    consumer.close()
  }
}
